﻿using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading;
using AutoMapper;
using DefaultConnection;
using MementoLibrary.DTOs;
using MementoWeb.Controllers;
using MementoWeb.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace MementoWeb.Tests.Controllers
{
    [TestClass]
    public class TaskControllerTest
    {
        private Mock<IDataRepository> _dataRepository;
        private TaskController _controller = null;

        [TestInitialize]
        public void Setup()
        {
            AutomapperConfig.RegisterAutomapperMaps();
            _dataRepository = new Mock<IDataRepository>();
        }


        [TestMethod]
        public void GetAllTasksInRepository()
        {
            // Arrange
            var tasks = new List<Task>
                            {
                               new Task
                                   {
                                       TaskId = 1,
                                       ProjectId = 1,
                                       SourceSystem = "TFS100",
                                       Name = "",
                                       Description = "",
                                       CreatedBy = "system",
                                       DateCreated = DateTime.UtcNow,
                                       UpdatedBy = "system",
                                       DateUpdated = DateTime.UtcNow
                                   },
                                new Task
                                   {
                                       TaskId = 2,
                                       ProjectId = 1,
                                       SourceSystem = "TFS100",
                                       Name = "",
                                       Description = "",
                                       CreatedBy = "system",
                                       DateCreated = DateTime.UtcNow,
                                       UpdatedBy = "system",
                                       DateUpdated = DateTime.UtcNow
                                   }
                            };
            _dataRepository.Setup(r => r.GetAllTasks()).Returns(tasks);
            _controller = new TaskController(_dataRepository.Object);
            // Act
            var result = _controller.Get();

            // Assert
            Assert.IsNotNull(result);
            _dataRepository.Verify(r => r.GetAllTasks(), Times.AtLeastOnce());
        }

        [TestMethod]
        public void GetATaskById()
        {
            var task = new Task
                           {
                               TaskId = 1,
                               ProjectId = 1,
                               SourceSystem = "TFS100",
                               Name = "",
                               Description = "",
                               CreatedBy = "system",
                               DateCreated = DateTime.UtcNow,
                               UpdatedBy = "system",
                               DateUpdated = DateTime.UtcNow
                           };
            _dataRepository.Setup(r => r.GetTaskById(1)).Returns(task);
            _controller = new TaskController(_dataRepository.Object);
            var result = _controller.GetById(1);
            Assert.IsNotNull(result);
            _dataRepository.VerifyAll();
        }

        [TestMethod, Ignore]
        public void CreateANewTask()
        {
            var project = new ProjectDTO
            {
                Name = "ProjectName",
                Description = "Description",
                SourceSystem = "TFS100"
            };
            var task = new TaskDTO
            {
                Name = "DoWork",
                Description = "",
                Project = project
            };

            var taskSaved = new Task();
            _dataRepository.Setup(r => r.CreateNewTask(It.IsAny<Task>()))
                .Callback<Task>(t => taskSaved = t);
            //_controller.CreateNew(task);

        }

        [TestMethod, Ignore]
        public void UpdateATask()
        {
        }

        [TestMethod, Ignore]
        public void DeleteATask()
        {
        }
    }
}
